﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AssetManagementSystem.Utils;
using AssetManagementSystem.Model.IDao;

namespace AssetManagementSystem.Model {
    class GroupManager {

        IGroupDAO dao;

        public GroupManager() {
            //get the group dao
            dao = DAOManager.GetGroupDAO();
        }

        //====================================================================
        //  STANDARD CRUD METHODS
        //====================================================================

        public void CreateGroup(Group newGroup) {
            dao.CreateGroup(newGroup);
        }

        public List<Group> GetAllGroups() {
            return dao.GetAllGroups().ToList<Group>();
        }

        public Group GetGroup(string id) {
            return dao.GetGroup(id);
        }

        public void UpdateGroup(Group existingGroup) {
            dao.UpdateGroup(existingGroup);
        }

        public void DeleteGroup(Group group) {
            dao.DeleteGroup(group);
        }

        public void DeleteGroupProperties(string groupName, List<string> properties) {
            //delete every property found in the dictionary
            foreach (string property in properties) {
                dao.DeleteGroupProperty(groupName, property);
            }
        }

        //====================================================================
        //  END - STANDARD CRUD METHODS
        //====================================================================

        //====================================================================
        //  ENHANCED METHODS
        //====================================================================


        /// <summary>
        /// Attempts to find groups contain part, or all of the input text in their string representations (ToString()).
        /// The results are sorted by relevance.
        /// </summary>
        /// <param name="input">The text to search for</param>
        /// <returns>The list of sorted results</returns>
        public List<Group> FindGroups(string input) {
            //Get all groups
            List<Group> allGroups = GetAllGroups();
            //Create a list to store results
            List<SortObject> sortList = new List<SortObject>();
            //Get all groups with at least one tri-gram match to the input
            foreach (Group group in allGroups) {
                int ngramMatches = SearchUtil.NGramSearch(input, group.ToString(), 3);
                if(ngramMatches > 0){
                    sortList.Add(new SortObject(ngramMatches, group));
                }
            }
            //Sort the results by the number of matches they each had
            sortList.Sort();
            //Turn the results back into a list of groups
            List<Group> matchedGroups = new List<Group>();
            foreach (SortObject obj in sortList) {
                matchedGroups.Add((Group)obj.TheObject);
            }
            //return the sorted results
            return matchedGroups;
        }

        //====================================================================
        //  END - ENHANCED METHODS
        //====================================================================
    }
}
